<template>
	<view class="register-container">
    <!-- 填充区 -->
    <view class="statusBar" :style="{ height: getStatusBarHeight()+ 'px'}"></view>
		<view class="register-header">
			<text class="register-title">创建账号</text>
			<text class="register-subtitle">请填写以下信息完成注册</text>
		</view>
		
		<view class="form-container">
			<view class="input-group">
				<text class="label">用户名</text>
				<input class="input" type="text" v-model="formData.username" placeholder="请输入用户名" />
			</view>
			
			<view class="input-group">
				<text class="label">手机号码</text>
				<input class="input" type="number" v-model="formData.phone" placeholder="请输入手机号码" maxlength="11" />
			</view>
			
			<view class="input-group">
				<text class="label">身份证号</text>
				<input class="input" type="text" v-model="formData.idCard" placeholder="请输入身份证号" maxlength="18" />
			</view>
			
			<view class="input-group">
				<text class="label">密码</text>
				<input class="input" type="password" v-model="formData.password" placeholder="请输入��码" password />
			</view>
			
			<view class="input-group">
				<text class="label">确认密码</text>
				<input class="input" type="password" v-model="formData.confirmPassword" placeholder="请再次输入密码" password />
			</view>
			
			<button class="register-btn" @click="handleRegister">注册</button>
			
			<view class="login-link" @click="navigateToLogin">
				<text>已有账号？</text>
				<text class="link">立即登录</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { reactive } from 'vue'
import { getStatusBarHeight } from '@/utils/system'
import { postRegisterAPI } from '@/services/login-register'
import { useUserStore } from '@/stores'

const useInfoStore = useUserStore()
//表单信息
const formData = reactive({
	username: '',
	phone: '',
	idCard: '',
	password: '',
	confirmPassword: ''
})

//封装显示toast
const showToast = (title) => {
	uni.showToast({
		title: title,
		icon: 'none'
	})
}

//表单验证
const validateForm = () => {
	if (!formData.username) {
		showToast('请输入用户名')
		return false
	}
	if (!formData.phone) {
		showToast('请输入手机号码')
		return false
	}
	if (!/^1\d{10}$/.test(formData.phone)) {
		showToast('请输入正确的手机号码')
		return false
	}
	if (!formData.idCard) {
		showToast('请输入身份证号')
		return false
	}
	if (!/^\d{17}[\dXx]$/.test(formData.idCard)) {
		showToast('请输入正确的身份证号')
		return false
	}
	if (!formData.password) {
		showToast('请输入密码')
		return false
	}
	if (formData.password.length < 6) {
		showToast('密码长度不能少于6位')
		return false
	}
	if (formData.password !== formData.confirmPassword) {
		showToast('两次输入的密码不一致')
		return false
	}
	return true
}

//注册
const handleRegister = async () => {
	if (!validateForm()) return
	console.log(formData)
	uni.showLoading({
		title: '注册中...'
	})
	const res = await postRegisterAPI(formData)
	console.log('register',res)
	if(res.msg && res.msg === '注册成功'){
		useInfoStore.setUserInfo(formData)
		uni.hideLoading()
		uni.showToast({
			title: '注册成功',
			icon: 'success',
			duration: 2000,
			success: () => {
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}, 1200)
			}
		})
	}
}

//返回登录页面
const navigateToLogin = () => {
  uni.navigateTo({
    url: '/pages/login/login'
  })
}
</script>

<style lang="scss">
.register-container {
	padding: 40rpx;
	height: 100vh;
	background-color: $uni-bg-color;
  .statusBar{
    width: 100%;
    background-color: $uni-bg-color;
  }
	.register-header {
		margin: 60rpx 0;
		text-align: center;
		
		.register-title {
			font-size: 48rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 20rpx;
			display: block;
		}
		
		.register-subtitle {
			font-size: 28rpx;
			color: #666666;
		}
	}
	
	.form-container {
		margin-top: 60rpx;
		
		.input-group {
			margin-bottom: 40rpx;
			
			.label {
				font-size: 28rpx;
				color: #333333;
				margin-bottom: 16rpx;
				display: block;
			}
			
			.input {
				height: 90rpx;
				background-color: $uni-bg-color-grey;
				border-radius: 45rpx;
				padding: 0 40rpx;
				font-size: 28rpx;
			}
		}
		
		.register-btn {
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			background-color: #007AFF;
			color: $uni-bg-color;
			border-radius: 45rpx;
			font-size: 32rpx;
			font-weight: bold;
			margin: 60rpx 0 40rpx;
		}
		
		.login-link {
			text-align: center;
			font-size: 26rpx;
			
			text {
				color: #666666;
				
				&.link {
					color: #007AFF;
					margin-left: 10rpx;
				}
			}
		}
	}
}
</style> 